package com.xiaoxiang.cameracontrol;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.xiaoxiang.cameracontrol.zsocket.SocketClient;

public class LoginActivity extends AppCompatActivity {

    private EditText server_ip;
    private EditText server_port;
    private EditText device_id;
    private Button connect_button;
    public static CameraSocket socket_client;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getSupportActionBar() != null){
            getSupportActionBar().hide();
        }
        setContentView(R.layout.activity_login);

        this.server_ip = findViewById(R.id.server_ip);
        this.server_port = findViewById(R.id.server_port);
        this.device_id = findViewById(R.id.device_id);
        this.connect_button = findViewById(R.id.connect_button);

        this.initConnectButton();
        //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    }

    private void initConnectButton(){
        this.connect_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String ip = server_ip.getText().toString();
                String port = server_port.getText().toString();
                String my_id = device_id.getText().toString();
                connect_button.setText("正在连接...");
                connect_button.setEnabled(false);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        socket_client = new CameraSocket(handler);
                        socket_client.connect(ip, Integer.parseInt(port));
                        socket_client.send_auth(my_id);
                    }
                }).start();

            }
        });
    }

    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message message) {
            switch(message.what){
                case 0:
                    loginSuccess();
                    break;
                case 1:
                    loginFailed();
                    break;

            }
            return false;
        }
    }
    );

    private void loginSuccess(){
        this.connect_button.setText("连接到服务器");
        this.connect_button.setEnabled(true);
        Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();

        Intent intent = new Intent(this, MainActivity2.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);

    }

    private void loginFailed(){
        socket_client.close();
        Toast.makeText(this, "登录失败，设备ID被占用", Toast.LENGTH_SHORT).show();
        this.connect_button.setText("连接到服务器");
        this.connect_button.setEnabled(true);
    }



}